package com.soul.dubbo.api.mongo;

import com.soul.dubbo.utils.IdWorker;
import com.soul.model.mongo.Peachblossom;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.RandomUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.FindAndModifyOptions;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.data.mongodb.core.query.UpdateDefinition;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * @author mdoya
 * @version 1.0
 * @description:
 */
@Slf4j
@DubboService
public class PeachblossomApiImpl implements PeachblossomApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    protected IdWorker idWorker;

    /**
     * 保存语音文件
     *
     * @param peachblossom 文件对象
     */
    @Override
    public Integer addPeachblossom(Peachblossom peachblossom) {
        // 设置发语音文件时间，执行语句
        long nowTime = System.currentTimeMillis();
        peachblossom.setCreated(nowTime);
        peachblossom.setPeachBlossomId(peachblossom.getUserId()); // 给谁发了桃花传音
        mongoTemplate.insert(peachblossom);
        // 返回剩余次数
        Query query = new Query();
        query.addCriteria(Criteria.where("userId").is(peachblossom.getUserId()));
        return mongoTemplate.find(query, Peachblossom.class).size();
    }

    /**
     * 查询桃花传音用户语音文件
     *
     * @param peachblossomId 大数据推荐桃花传音要用户id
     * @return List<Peachblossom>
     */
    @Override
    public List<Peachblossom> findPeachblossom(Long peachblossomId) {
        Query query = new Query();
        query.addCriteria(Criteria.where("userId").is(peachblossomId));
        return mongoTemplate.find(query,Peachblossom.class);
    }

    /**
     * 查询当前用户已用桃花传音次数
     *
     * @param currendUserId 当前用户id
     * @return List<Peachblossom>
     */
    @Override
    public List<Peachblossom> findPeachblossomCount(Long currendUserId) {
        Query query = new Query();
        query.addCriteria(Criteria.where("userId").is(currendUserId));
        return mongoTemplate.find(query,Peachblossom.class);
    }

    /**
     * 从语音库随机获取一个桃花传音用户
     *
     * @return Peachblossom
     */
    @Override
    public Peachblossom findByPeachblossom(Long currendUserId) {
        // 1. 查询所有桃花传音用户
        List<Peachblossom> peachblossomList = mongoTemplate.findAll(Peachblossom.class);
        // 2. 随机获取一个桃花传音用户userId(字段)
        int peachblossomUserId = RandomUtils.nextInt(0, peachblossomList.size() - 1);
        System.err.println("桃花传音随机号：" +peachblossomUserId);
        // 3. 查询桃花传音用户 可能存在一个用户发多条语音
        if (CollectionUtils.isEmpty(peachblossomList)){
            return null;
        }
        // 3. 非自己  非同性别
        for (int i = 0; i < peachblossomList.size(); i++) {
            Peachblossom peachblossom = peachblossomList.get(peachblossomUserId);
            if (peachblossom != null){
                return peachblossom;
            }
        }
/*        Query query = new Query();
        query.addCriteria(Criteria.where("count").is(String.valueOf(peachblossomUserId)));
        Peachblossom peachblossom = mongoTemplate.findOne(query, Peachblossom.class);
        List<Peachblossom> peachblossomList2 = mongoTemplate.findOne(query, Peachblossom.class);
        if (CollectionUtils.isEmpty(peachblossomList2)){
            return null;
        }
        // 4. 如果获取第一次拿到语音URL直接返回对象，没有找到继续遍历
        for (Peachblossom peachblossom : peachblossomList2) {
            if (peachblossom.getSoundFileUrl() != null){
                return peachblossom;
            }
        }
        // 5. 获取不到语音URL返回空*/
        return null;
    }
}
